<?php
// +----------------------------------------------------------------------
// | 迁于首页
// +----------------------------------------------------------------------
// | Author: cart <469714167@qq.com>
// +----------------------------------------------------------------------
// | Datetime: 2018-5-16 14:59:00
// +----------------------------------------------------------------------
// | Copyright: 创讯科技
// +----------------------------------------------------------------------
namespace app\ice\controller;
use \think\Controller;
use think\Loader;
use think\Log;
use app\ice\model\Area;
class Index extends Init
{
    // private $IndexModel;
    /**
     * [_initialize 初始化]
     * @return [type] [description]
     * @author carl
     * @version 2018-02-05
     * @copyright wei liang
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->UserModel = Loader::model('User');
        $this->IndexModel = Loader::model('Index');
    }

    public function index(){
      	$param = input('post.');

      	$page = $param['page'] ? $param['page'] : 1;
      	$orll_page = config('paginate.list_rows'); 
      	$res = $this->IndexModel->getList($param,$page,$orll_page);
      	if($res){
        	return ['data' => $res, 'code' => 200, 'message' => '列表显示成功', 'status' => true ];
      	}else{
        	return ['data' => [], 'code' => 400, 'message' => '已经到底了哦', 'status' => true ];
      	}
    }

    public function detail(){
      $param = input('post.');
      // $Model = new Index();
      $page = $param['page'] ? $param['page'] : 1;
      $orll_page = config('paginate.list_rows'); 
      $res = $this->IndexModel->getList($param,$page,$orll_page);
      if($res){
        return ['data' => $res, 'code' => 200, 'message' => '列表显示成功', 'status' => true ];
      }else{
        return ['data' => [], 'code' => 400, 'message' => '已经到底了哦', 'status' => true ];
      }
    }

    public function onlogin(){
        $code = input('get.code');
        $appid= 'wxa20052cca5271e47';
        $secret= 'e66c2c6abc2502832a62e4bbb3e708d6';
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';
        $result = http_get($url);
        if ($result)
        {
            $json = json_decode($result,true);
            if (!$json || isset($json['errcode'])) {
                $errCode = $json['errcode'];
                $errMsg = $json['errmsg'];
                return false;
            }
            $session_key = $json['session_key'];
            $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
            $this->setCache($authname,$session_key,$expire);
            $userInfo = $this->UserModel->getUserInfo($json['openid']);
            if(!$userInfo){
                $this->UserModel->openid = $json['openid'];
                $this->UserModel->save();
            }
            return $session_key;
        }
    }

    public function Wechat() {
        $this->weObj->valid(); // 注意, 应用验证通过后,可将此句注释掉, 但会降低网站安全性
        echo 11;exit;
        var_dump($this->weObj->getRev ());
        exit;
        $type = $this->weObj->getRev ()->getRevType ();
        var_dump($type);
        exit;
        $Rev = $this->weObj->getRev();
        Log::record($type);
        switch ($type) {
        // 收到文本信息事件
          case Wechat::MSGTYPE_TEXT :
            $user = $this->weObj->getRev ()->getRevFrom ();
            $publicid = $this->weObj->getRev ()->getRevTo (); // 获取消息接受者ID
            $msgid = $this->weObj->getRev ()->getRevID (); // 获取消息ID
            $content = $this->weObj->getRev ()->getRevContent (); // 获取消息内容
            break;
          case Wechat::MSGTYPE_IMAGE :
            /*$picture = $this->weObj->getRev ()->getRevPic ();
            $data = getHttpHead ( $picture );
            $this->weObj->text ( $data )->reply ();
            exit ();*/
            break;
          case Wechat::MSGTYPE_LOCATION :
            break;
          case Wechat::MSGTYPE_LINK :
            break;
          case Wechat::MSGTYPE_EVENT :
          $user = $this->weObj->getRev ()->getRevFrom (); // 获得关注用户的openID
          $event = $this->weObj->getRevEvent ();
          $userdata = $this->weObj->getUserInfo ( $user ); // 获取用户的信息
          // 事件类型，有subscribe(订阅),unsubscribe(取消订阅),LOCATION(上报地理位置),CLICK(自定义菜单)
          $eventtype = $event ['event'];
          // 事件类型，有subscribe(订阅),unsubscribe(取消订阅),LOCATION(上报地理位置),CLICK(自定义菜单)
          // 事件KEY值，与自定义菜单接口中KEY值对应
          $eventkey = $event ['key'];
          $content = 'welcome';
        break;
        //       $repeat = $keys['d_content'];
        //     }
        // $this->weObj->text ( $repeat )->reply ();
          break;
          // 用户取消关注
          //         'd_subscribe' => ( int ) 0 
          //     )
          // );
          // D ( 'User_info' )->update ( $update );
          break;
          // TODO:自定义菜单click事件
          // TODO:自定义菜单view事件
        // $this->weObj->sendCustomMessage($kf_msg);
        // $content = 'sub_repeat';
        // $keys = D('Flow')->getKeyData($content);
        // if ($content == $keys['d_key']) {
        //       $repeat = $keys['d_content'];
        //     }
        // $this->weObj->text ( $repeat )->reply ();
        break;
        case Wechat::MSGTYPE_MUSIC :
          break;
        case Wechat::MSGTYPE_NEWS :
          break;
        case Wechat::MSGTYPE_VOICE :
          break;
        case Wechat::MSGTYPE_VIDEO :
          break;
        default :
        $this->weObj->text ( "help info" )->reply ();
      }
    }

    protected function setCache($cachename,$value,$expired){
        //TODO: set cache implementation
        return Cache::set($cachename,$value,$expired);
    }

}